/**
 * 
 */
package gpl.androidcommons.app;

import java.util.HashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import android.app.Application;
import android.util.Log;

/** @author norbert */
public class NApplication extends Application {

  private static final String TAG = Application.class.toString();

  private static final HashMap<String, Object> POST_ARGS = new HashMap<String, Object>(50);

  private static final ScheduledThreadPoolExecutor THREADS_EXECUTOR = new ScheduledThreadPoolExecutor(3);

  @Override
  public void onCreate() {
    super.onCreate();
  }

  /** Maps the specified key to the specified value. Override existing value.
   * @param key
   * @param obj
   */
  public static void push(String key, Object obj) {
    POST_ARGS.put(key, obj);
    Log.d(TAG, String.format("push  key: " + key + " obj: " + obj));
  }

  /** Removes the mapping with the specified key from this map.
   * @param key
   * @param clazz cast result
   * @return removed obj for the given key.
   */
  @SuppressWarnings("unchecked")
  public static <T> T pull(String key, Class<T> clazz) {
    Log.d(TAG, String.format("pull key: " + key + " clazz: " + clazz));
    return (T) POST_ARGS.remove(key);
  }

  /** Runs command at the given delay.
   * @param command
   * @param delay MILLISECONDS
   */
  public static void schedule(Runnable command, int delay) {
    THREADS_EXECUTOR.schedule(command, delay, TimeUnit.MILLISECONDS);
  }

  /** Runs command.
   * @param runnable
   */
  public static void execute(Runnable command) {
    THREADS_EXECUTOR.execute(command);
  }
}
